package com.ui;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bean.MenuTextBean;
import com.ddd.wandemo.R;
import com.widget.MenuItemView;

import java.util.ArrayList;
import java.util.List;

public class TestRvAdapter extends RecyclerView.Adapter<TestRvAdapter.TestArticleViewHolder> {
    private Context mContext;
    private List<MenuTextBean> mData = new ArrayList<>();

    public TestRvAdapter(Context context) {
        mContext = context;
    }

    @NonNull
    @Override
    public TestRvAdapter.TestArticleViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new TestArticleViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_test, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull final TestRvAdapter.TestArticleViewHolder holder, final int position) {
        holder.tvPrice.setText(mData.get(position).getPrice());
        holder.tvContent.setText(mData.get(position).getContent());
        holder.tvTitle.setText(mData.get(position).getTitle());

        ((MenuItemView)holder.itemView).setItemView(mData.get(position).getCount());
        holder.mIvDecrease.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ((MenuItemView)holder.itemView).decrease();
                mData.get(position).setCount(((MenuItemView)holder.itemView).getCount());
            }
        });
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ((MenuItemView)holder.itemView).addCount();
                mData.get(position).setCount(((MenuItemView)holder.itemView).getCount());
            }
        });
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    public class TestArticleViewHolder extends RecyclerView.ViewHolder {
        private TextView tvTitle, tvContent, tvPrice, tvCount;
        private ImageView mIvDecrease;

        public TestArticleViewHolder(View itemView) {
            super(itemView);
            tvCount = itemView.findViewById(R.id.count);
            tvContent = itemView.findViewById(R.id.content);
            tvTitle = itemView.findViewById(R.id.tv_title);
            tvPrice = itemView.findViewById(R.id.tv_price);
            mIvDecrease = itemView.findViewById(R.id.iv_decrease);
        }
    }

    public void setData(List<MenuTextBean> data) {
        if (mData != null) {
            mData.clear();
            mData.addAll(data);
        }
        notifyDataSetChanged();
    }
}
